package com.xls.dormitoryservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.exception.BusinessException;
import com.xls.dormitoryservice.entity.Notice;
import com.xls.dormitoryservice.entity.Room;
import com.xls.base.model.Student;
import com.xls.dormitoryservice.mapper.NoticeMapper;
import com.xls.dormitoryservice.mapper.RoomMapper;
import com.xls.base.mapper.StudentMapper;
import com.xls.dormitoryservice.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xls.dormitoryservice.vo.NoticeQuery;
import com.xls.entity.SecurityUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public boolean saveNotice(Notice notice) {
        return this.save(notice);
    }

    @Override
    public boolean updateNotice(Notice notice) {
        return this.updateById(notice);
    }

    @Override
    public Page<Notice> getNoticePage(Integer page, Integer limit, NoticeQuery noticeQuery) {
        QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
        String building = noticeQuery.getBuilding();
        if ("-1".equals(noticeQuery.getBuilding())) {
            SecurityUser securityUser = (SecurityUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = securityUser.getUsername();
            Student student = studentMapper.selectOne(new QueryWrapper<Student>().eq("sno", username));
            if (student == null) {
                throw new BusinessException(8888, "管理员不允许操作");
            }
            if (student.getRoomId() == null) {
                throw new BusinessException(8888, "请先入住");
            }
            Room room = roomMapper.selectById(student.getRoomId());
            building = room.getBuilding();
        }
        Integer importance = noticeQuery.getImportance();
        String title = noticeQuery.getTitle();
        String begin = noticeQuery.getBegin();
        String end = noticeQuery.getEnd();
        noticeQueryWrapper.eq(!StringUtils.isEmpty(building), "building", building)
                .eq(!StringUtils.isEmpty(importance), "importance", importance)
                .likeRight(!StringUtils.isEmpty(title), "title", title)
                .gt(!StringUtils.isEmpty(begin), "publish_time", begin)
                .lt(!StringUtils.isEmpty(end), "publish_time", end)
                .eq("is_deleted", 0);
        return noticeMapper.getNoticePage(new Page<>(page, limit), noticeQueryWrapper);
    }
}
